package com.cmpp.client.util;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
//import java.util.ArrayList;
//import java.util.List;

import com.cmpp.client.domain.*;


public class CMPPContainer {
    //public MsgConfig msgConfig;
    public  Socket msgSocket;
    public  DataInputStream in;
    public static  DataOutputStream out;
    
    public  Socket getSocketInstance(){
        //if(null==msgSocket||msgSocket.isClosed()||!msgSocket.isConnected()){
            System.out.println("**********************开始建立socket链接");
            try {
                in=null;
                out=null;
                msgSocket=new Socket(CMPPConfig.ServerIp,CMPPConfig.ServerPort);
                msgSocket.setKeepAlive(true);
                //in=in;
                //out=out;
                in=new DataInputStream(msgSocket.getInputStream());
                out=new DataOutputStream(msgSocket.getOutputStream());
                System.out.println("**********************建立socket链接成功");
                connectISMG();
                /*int count=0;
                boolean result=connectISMG();
                while(!result){
                    count++;
                    result=connectISMG();
                    if(count>=(msgConfig.getConnectCount()-1)){//如果再次连接次数超过两次则终止连接
                        break;
                    }
                }*/
            } catch (Exception e) {
                System.out.println("Socket链接短信网关失败："+e.getMessage());
            }
        //}
        return msgSocket;
    }
    
    /**
     * 创建Socket链接后请求链接ISMG
     * @return
     */
    public   boolean connectISMG(){
        System.out.println("************************注册请求===========");
        CMPPConnect connect=new CMPPConnect();
        connect.setTotal_Length(12+6+16+1+4);//消息总长度，级总字节数:4+4+4(消息头)+6+16+1+4(消息主体)
        connect.setCommand_Id(CMPPDefine.CMPP_CONNECT);//标识创建连接
        connect.setSequence_Id(CMPPUtils.getSequence());//序列，由我们指定
        connect.setSourceAddr(CMPPConfig.CompanyCode);//我们的企业代码
        connect.setAuthenticatorSource(CMPPUtils.md5(CMPPConfig.CompanyCode,CMPPConfig.Pwd));//md5(企业代码+密匙+时间戳)
        connect.setTimestamp(Integer.parseInt(CMPPUtils.getTimestamp()));//时间戳(MMDDHHMMSS)
        connect.setVersion((byte)0x30);//版本号 高4bit为3，低4位为0
//      List<byte[]> dataList=new ArrayList<byte[]>();
//      dataList.add(connect.toByteArry());
        CMPPSend sender=new CMPPSend(out,connect.toByteArry());
        try {
            sender.start();
            System.out.println("************************注册请求完毕===========");
            return true;
        } catch (Exception e) {
            try {
                out.close();
            } catch (IOException e1) {
                out=null;
            }
            return false;
        }
    }
    public  void sendShortMsg(String msg, String number) {
        try {
            int seq = CMPPDefine.getSequence();
            CMPPSubmit submit = new CMPPSubmit();
            submit.setTotal_Length(12 + 8 + 1 + 1 + 1 + 1 + 10 + 1 + 32 + 1 + 1
                    + 1 + 1 + 6 + 2 + 6 + 17 + 17 + 21 + 1 + 32 + 1 + 1
                    + msg.length() * 2 + 20);
            submit.setCommand_Id(CMPPDefine.CMPP_SUBMIT);
            submit.setSequence_Id(seq);
            submit.setPkTotal((byte) 0x01);
            submit.setPkNumber((byte) 0x01);
            submit.setRegisteredDelivery((byte) 0x00);
            submit.setMsgLevel((byte) 0x01);
            submit.setFeeUserType((byte) 0x02);
            submit.setFeeTerminalId("");
            submit.setFeeTerminalType((byte) 0x00);
            submit.setTpPId((byte) 0x00);
            submit.setTpUdhi((byte) 0x00);
            submit.setMsgFmt((byte) 0x0F);
            submit.setMsgSrc(CMPPConfig.CompanyCode);
            submit.setSrcId(CMPPConfig.ServiceCode);
            submit.setDestTerminalId(number);
            submit.setMsgLength((byte) (msg.length() * 2));
            submit.setMsgContent(msg.getBytes("gb2312"));
            submit.setServiceId(CMPPConfig.BusinessCode);

            //List<byte[]> dataList = new ArrayList<byte[]>();
            //dataList.add(submit.toByteArry());
            CMPPSend sender = new CMPPSend(out, submit.toByteArry());
            sender.start();
            System.out.println("向手机号码："+number+"下发短短信，序号为:"+seq);
            //return sender.start();
        } catch (Exception e) {
            try {
                out.close();
            } catch (IOException e1) {
                out = null;
            }
            System.out.println("发送短短信" + e.getMessage());
            //return -1;
        }
    }

}
